package library;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.Subject;

import kr.co.exight.onApp.ActivityTimeDetail;
import kr.co.exight.onApp.ActivityTimeTable;

import struct.SubjectInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyTimeData extends SQLiteOpenHelper{

	String dbName = "";	
	public static final String DBNAME_TIMETABLE= "timeTable";
	
	public MyTimeData(String dbName, Context context) {
		super(context, dbName, null, 2);
		// TODO Auto-generated constructor stub
		this.dbName = dbName;
		
	}
	
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		if(dbName.equals(DBNAME_TIMETABLE)){
			String query = "CREATE TABLE " + dbName + "(" +
			"number INTEGER PRIMARY KEY AUTOINCREMENT, " +
			"name TEXT, " +
			"site TEXT, " +
			"arrTime TEXT, " +
			"day TEXT);";
			
			db.execSQL(query);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	public void pushTableDataToDB(String dbName, HashMap<String, ArrayList<SubjectInfo>> hashData){
		
		SQLiteDatabase db = getWritableDatabase();
		ContentValues row = null;
		
		for(Map.Entry data : hashData.entrySet()){
			ArrayList<SubjectInfo> arrSub = (ArrayList<SubjectInfo>)data.getValue();
			
			if(arrSub == null) continue;
			
			int size = arrSub.size();
			
			for(int i = 0; i<size; i++){
				row = new ContentValues();
				row.put("name", arrSub.get(i).getName());
				row.put("site", arrSub.get(i).getSite());
				row.put("arrTime", arrSub.get(i).getArrTime().toString());
				row.put("day", (String) data.getKey());
				
				long ret = db.insert(dbName, null, row);
			}
		}
	}
	
	
	public void removeRow(String dbName){
		SQLiteDatabase db = getWritableDatabase();
		String strQuery = "DELETE FROM " + dbName;
		db.execSQL(strQuery);
		close();
	}

	public HashMap<String , ArrayList<SubjectInfo>> getResDataFromDB(){
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery("SELECT * FROM " + DBNAME_TIMETABLE, null);
		
		HashMap<String, ArrayList<SubjectInfo>> hashData = new HashMap<String, ArrayList<SubjectInfo>>();
		ArrayList<String> arr = new ArrayList<String>();
		arr.add("월"); arr.add("화"); arr.add("수"); arr.add("목"); arr.add("금");
		int maxTimeLine = 12;
		
		while (cursor.moveToNext()){
			SubjectInfo sub = new SubjectInfo();
			sub.setName(cursor.getString(1));
			sub.setSite(cursor.getString(2));
			
			String[] timearr = cursor.getString(3).trim().split(", ");
			timearr[0] = timearr[0].substring(1);
			timearr[timearr.length-1] = timearr[timearr.length-1].substring(0,timearr[timearr.length-1].length()-1); // toString 한 arr값 다시 넣기
			
			ArrayList<Integer> arrInt = new ArrayList<Integer>();
			sub.setArrTime(arrInt);
			
			for(int i = 0 ; i < timearr.length; i ++){
				arrInt.add(Integer.parseInt(timearr[i]));
				if(arrInt.get(i) > maxTimeLine) maxTimeLine = arrInt.get(i);
			}
			
			String day = cursor.getString(4);
			
			if(hashData.get(day) == null){
				ArrayList<SubjectInfo> arrSub = new ArrayList<SubjectInfo>();
				arrSub.add(sub);
				hashData.put(day, arrSub);
			}else {
				hashData.get(day).add(sub);
			}
			arr.remove(day);
			
		}
		for(String a : arr){
			hashData.put(a, null);
		}
		
		close();
		ActivityTimeTable.maxTimeLine = maxTimeLine;
		return hashData;
	}

}
